﻿using StudentManage.ViewModels.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zhy.DAL.Models;
using Zhy.DAL;
using Microsoft.IdentityModel.Tokens;
using System.Linq.Expressions;

namespace StudentManage.ViewModels
{
    public class MainViewModel : ViewModelBase
    {
        #region 字段
        /// <summary>
        /// 数据操作上下文
        /// </summary>
        ZhyDbContext context = new ZhyDbContext();
        #endregion

        /// <summary>
        /// 初始化
        /// </summary>
        public MainViewModel()
        {

        }

        #region 属性
        /// <summary>
        /// 用户信息
        /// </summary>
        private UserInfo userInfo = new UserInfo();

        /// <summary>
        /// 用户名
        /// </summary>
        public string UserName
        {
            get { return userInfo.UserName; }
            set
            {
                userInfo.UserName = value;
            }
        }

        /// <summary>
        /// 密码
        /// </summary>
        public string UserPwd
        {
            get { return userInfo.UserPwd; }
            set
            {
                userInfo.UserPwd = value;
            }
        }


        #endregion

        #region 命令
        /// <summary>
        /// 登陆按钮
        /// </summary>
        public RelayCommand LoginCmd
        {
            get
            {
                return new RelayCommand(o =>
                {
                    if (userInfo.UserName.IsNullOrEmpty())
                    {
                        ShowQuestion("登陆提示", "用户名为空");
                        return;
                    }
                    else if (userInfo.UserPwd.IsNullOrEmpty())
                    {
                        ShowQuestion("登陆提示", "用户密码为空");
                        return;
                    }

                    ////查询
                    var user = context.UserInfo.FirstOrDefault(c => c.UserName.Equals(userInfo.UserName)
                    && c.UserPwd.Equals(userInfo.UserPwd));

                    //检查账号密码-- 检查是否为空 可以省略
                    if (user != null)
                    {
                        StudentMainViewModel vm = new StudentMainViewModel(UserName);
                        Show("stuMainWin", vm);//显示窗口
                    }
                    else
                    {
                        ShowError("登录提示", "账号或密码输入有误！");
                        return;
                    }
                }
                 , b =>
                 {
                     bool bl = (string.IsNullOrEmpty(UserName) || string.IsNullOrEmpty(UserPwd)) ? false : true;
                     return bl;
                     //return true;
                 }
                );
            }
        }

        /// <summary>
        /// 属性变更通知
        /// </summary>
        public RelayCommand NameChangedCmd
        {
            get
            {
                return new RelayCommand(o =>
                {
                    //LoginCmd.OnCanExecuteChanged(); // 用于判断登陆按钮是否可用
                });
            }
        }
        #endregion
    }
}
